package com.share.finance.asset.sjd.web;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.share.common.common.Page;
import com.share.common.common.ReqParam;
import com.share.common.common.Result;
import com.share.config.aop.decrypt.DecryptRequest;
import com.share.config.aop.encrypt.EncryptRespone;
import com.share.finance.asset.sjd.model.vo.EventPageReq;
import com.share.finance.asset.sjd.model.vo.EventReq;
import com.share.finance.asset.sjd.service.IEventInfoService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author author
 * @since 2023-11-29
 */
@Slf4j
@RestController
@RequestMapping("/event")
public class EventInfoController {
    @Autowired
    IEventInfoService eventInfoService;

    @ApiOperation("授信或用信事件")
    @RequestMapping("/apply/creditLoan")
    public Result applyCreditLoan(@RequestBody EventReq req, HttpServletRequest request) {
        log.info("接收到请求,参数req={}", JSON.toJSONString(req));
        Map<String, Object> data = req.getData();
        if (data == null) {
            return Result.failure("data不能为空");
        }

        if (data.get("idCard") == null || data.get("name") == null || data.get("phoneNo") == null) {
            return Result.failure("三要素存在空值");
        }

        return eventInfoService.applyCreditLoan(req, request);
    }

    @ApiOperation("风控决策结果-调用此接口-保存结果")
    @RequestMapping("/saveResult")
    public Result saveResult(@RequestBody JSONObject req) {
        log.info("回调保存结果,参数req={}", JSON.toJSONString(req));
        return Result.success(eventInfoService.saveResult(req));
    }

    @ApiOperation("分页查询")
    @RequestMapping(value = "/pageList")
    public Result logPageList(EventPageReq req) {
        Page page = eventInfoService.pageList(req);
        return Result.success(page);
    }


    @ApiOperation("加密测试")
    @RequestMapping(value = "/test")
    @DecryptRequest
    @EncryptRespone
    public Result logPageList1(@RequestBody ReqParam<EventPageReq> req) {
        Page page = eventInfoService.pageList(req.getUncrypt());
        return Result.success(page);
    }
}
